//
//  XMFiveViewController.m
//  gpos_app
//
//  Created by apple on 2018/8/14.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "XMFiveViewController.h"
#import "Masonry.h"
#import "MyTableViewCell.h"
#import "AboutUsViewController.h"
#import "SystemViewController.h"
#import "CustomerCenterViewController.h"
#import "MerchentEntryViewController.h"
#import "SafeCenterViewController.h"
#import "UserInfoViewController.h"
#import "UIImageView+WebCache.h"
#import "NoticeViewController.h"
#import "ShareViewController.h"
@interface XMFiveViewController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
@property (strong,nonatomic) UIImageView *topImage;
@property (strong,nonatomic) UIImageView *userImage;
@property (strong,nonatomic) UILabel *userName;
@property (strong,nonatomic) UITableView *MainTable;
@property (strong,nonatomic) UIButton *noticeBtn;

@end

@implementation XMFiveViewController
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    //接收通知改变昵称
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeusername:) name:@"changeUserName" object:nil];
   //接收通知改变头像
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeuserpict:) name:@"changeUserPict" object:nil];
}
-(void)changeusername:(NSNotification *)noti{
    
    self.userName.text=[NSString stringWithFormat:@"%@",[noti.userInfo objectForKey:@"userNames"]];
}
-(void)changeuserpict:(NSNotification *)no{
    
    UIImage *img=[no.userInfo objectForKey:@"userPict"];
    self.userImage.image=img;
    
}
-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
  

    [self initUI];
}
-(void)initUI{
    
    //背景图片
    self.topImage = [[UIImageView alloc] init];
    self.topImage.backgroundColor = [UIColor whiteColor];
    self.topImage.image = [UIImage imageNamed:@"beijing_wd"];
    self.topImage.userInteractionEnabled=YES;
    [self.view addSubview:self.topImage];
    if (IS_IPHONE5) {
        
        [self.topImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_top);
            make.left.equalTo(self.view.mas_left);
            make.right.equalTo(self.view.mas_right);
            make.height.equalTo(@(200));
        }];
        
    }else{
        
        [self.topImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_top);
            make.left.equalTo(self.view.mas_left);
            make.right.equalTo(self.view.mas_right);
            
        }];
        
    }
    //消息按钮
    self.noticeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.noticeBtn setImage:[UIImage imageNamed:@"icon_xiaoxi"] forState:UIControlStateNormal];
    [self.noticeBtn addTarget:self action:@selector(noticeView) forControlEvents:UIControlEventTouchUpInside];
    [self.topImage addSubview:self.noticeBtn];
    [self.noticeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.topImage.mas_top).offset(45);
        make.right.equalTo(self.topImage.mas_right).offset(-20);
        
    }];


    //用户头像
    self.userImage = [[UIImageView alloc] init];
    // 对URL字符串进行 URL 编码，避免中文乱码问题
    NSString *urlStr = [[[NSUserDefaults standardUserDefaults] objectForKey:@"portrait"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [self.userImage sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"icon_touxiang"]];
    self.userImage.layer.cornerRadius=40;
    self.userImage.clipsToBounds=YES;
    [self.topImage addSubview:self.userImage];
    self.userImage.userInteractionEnabled=YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTopView:)];
    tap.delegate=self;
    [self.userImage addGestureRecognizer:tap];
    CGFloat left_x = self.view.frame.size.width/2-40;
    if (IS_IPHONE5) {
        [self.userImage mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self.view.mas_top).offset(45);
            make.left.equalTo(self.view.mas_left).offset(left_x);
            make.width.equalTo(@(80));
            make.height.equalTo(@(80));
        }];
    }else{
        
        [self.userImage mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self.view.mas_top).offset(70);
            make.left.equalTo(self.view.mas_left).offset(left_x);
            make.width.equalTo(@(80));
            make.height.equalTo(@(80));
        }];
    }
    
    
    //用户名
    self.userName = [[UILabel alloc] init];
    self.userName.textColor=[UIColor whiteColor];
    self.userName.text = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"nickName"]];
    self.userName.textAlignment = NSTextAlignmentCenter;
    [self.topImage addSubview:self.userName];
    [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.userImage.mas_bottom).offset(10);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.equalTo(@(25));
        
    }];
    
    self.MainTable = [[UITableView alloc] init];
    self.MainTable.backgroundColor = [UIColor whiteColor];
    self.MainTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.MainTable.delegate=self;
    self.MainTable.dataSource=self;
//    self.MainTable.scrollEnabled = NO;
    self.MainTable.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.MainTable];
    
    CGFloat tabkeHeight=KmainScreenHeiht - kNavbarHeight - kBottomSafeHeight-82-self.topImage.frame.size.height-120;
    [self.MainTable mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.topImage.mas_bottom).offset(30);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
//        make.bottom.equalTo(self.view.mas_bottom).offset(-tabkeHeights);
        make.height.equalTo(@(tabkeHeight));
    }];
    
    
}
//用户头像的点击事件
-(void)clickTopView:(UIGestureRecognizer *)tp{
    
    UserInfoViewController *vc=[[UserInfoViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    vc.hidesBottomBarWhenPushed = NO;

    
}
//消息按钮的点击事件
-(void)noticeView{
    
    NoticeViewController *vc=[[NoticeViewController alloc] init];
    vc.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:vc animated:YES];
    vc.hidesBottomBarWhenPushed=NO;

}
#pragma tabledelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section==0)
    {
        return 2;
    }else{
        
        return 3;
    }
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section==0)
    {
        return nil;
    }else{
        
        UIView *view=[[UIView alloc] init];
        view.backgroundColor=[UIColor grayColor];
        view.alpha = 0.15;
        return view;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 55;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section ==0)
    {
        return 0;
    }else{
        
        return 15;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0)
    {
        MyTableViewCell *cell=[[[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil] firstObject];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.preservesSuperviewLayoutMargins = false;
        cell.separatorInset = UIEdgeInsetsZero;
        cell.layoutMargins = UIEdgeInsetsZero;
        if (indexPath.row==0)
        {
            cell.leftView.image=[UIImage imageNamed:@"icon_anquan"];
            cell.nameLab.text=@"安全中心";

        }else{
            
            cell.leftView.image=[UIImage imageNamed:@"icon_shezhi"];
            cell.nameLab.text=@"系统设置";
        }
        return cell;
    }else{
        
        
        MyTableViewCell *cell=[[[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil] firstObject];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.preservesSuperviewLayoutMargins = false;
        cell.separatorInset = UIEdgeInsetsZero;
        cell.layoutMargins = UIEdgeInsetsZero;
        if (indexPath.row==0) {
            cell.leftView.image=[UIImage imageNamed:@"icon_kefu"];
            cell.nameLab.text=@"客服中心";
        }else if(indexPath.row==1){
            
            
            cell.leftView.image=[UIImage imageNamed:@"icon_gywomen"];
            cell.nameLab.text=@"关于我们";
        }else{
            
            cell.leftView.image=[UIImage imageNamed:@"icon_fenxiang"];
            cell.nameLab.text=@"分享";
            
        }
        return cell;
    }
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0)
    {
        if (indexPath.row==0)
        {
           
            SafeCenterViewController *vc=[[SafeCenterViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:vc animated:YES];
            vc.hidesBottomBarWhenPushed = NO;
        
        }else{
            
            SystemViewController *vc=[[SystemViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
            vc.hidesBottomBarWhenPushed = NO;
            
        }
        
    }else{
        
        if (indexPath.row==0)
        {
            
            CustomerCenterViewController *vc=[[CustomerCenterViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
            vc.hidesBottomBarWhenPushed = NO;
            
        }else if (indexPath.row==1){
            
            AboutUsViewController *vc=[[AboutUsViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
            vc.hidesBottomBarWhenPushed = NO;
            
        }else{
            
            ShareViewController *vc=[[ShareViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
            vc.hidesBottomBarWhenPushed = NO;
        }
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
